package com.ruoyi.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.oms.entity.YytOmsOrder;
import com.ruoyi.common.oms.entity.YytOmsOrderReturnApply;
import com.ruoyi.common.oms.entity.YytOmsOrderReturnReason;
import com.ruoyi.common.oms.service.YytOmsOrderReturnApplyService;
import com.ruoyi.common.oms.service.YytOmsOrderReturnReasonService;
import com.ruoyi.common.oms.service.YytOmsOrderService;
import com.ruoyi.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/return")
public class OrderReturnController {

    @Autowired
    private YytOmsOrderReturnApplyService yytOmsOrderReturnApplyService;

    @Autowired
    private YytOmsOrderReturnReasonService yytOmsOrderReturnReasonService;

    @Autowired
    private YytOmsOrderService yytOmsOrderService;

    /**
     * 获取退款原因列表
     * @return
     */
    @GetMapping("/getReturnReason")
    public ResponseResult getReturnReason(){
        QueryWrapper<YytOmsOrderReturnReason> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0);
        return ResponseResult.success(yytOmsOrderReturnReasonService.list(wrapper));
    }

    /**
     * 退款申请
     * @param yytOmsOrderReturnApply
     * @return
     */
    @PostMapping("/getReturnApplyMoney")
    public ResponseResult getReturnApplyMoney(@RequestBody YytOmsOrderReturnApply yytOmsOrderReturnApply){
        UpdateWrapper<YytOmsOrder> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", yytOmsOrderReturnApply.getOrderId()).set("return_status", 1);
        yytOmsOrderService.update(wrapper);
        yytOmsOrderReturnApply.setCreateTime(LocalDateTime.now());
        return ResponseResult.success(yytOmsOrderReturnApplyService.save(yytOmsOrderReturnApply));
    }
}
